package com.xiaochong.bitong.api.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class RequestUtil {

    /**
     * 判断是否是需要验证的URL
     * @param path
     * @param suffix
     * @return
     */
    public static boolean parseSuffix(String path , String suffix){
        if(StringUtils.isBlank(path)){
            return false;
        }
        if(path.endsWith(suffix)){
            return true;
        }else {
            return false;
        }
    }

    /**
     * @Title: getHeaderNames
     * @Description: TODO(获取请求头部参数信息)
     * @param req
     * @return Map<String,String>    返回类型
     *
     *   token:访问token
        version : 客户端版本号
        model:手机型号   iphone6s
        client : IOS/Android
        longitude  经度
        latitude 纬度
        network   ： wifi  / 4G/3G
     *
     */
    public static Map<String,String> getHeaderNames(HttpServletRequest req){
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String key = (String) headerNames.nextElement();
            String value = req.getHeader(key);
            map.put(key, value);
        }
        return map;
    }

    /**
     *
     * @param req
     * @param findKey
     * @return
     */
    public static  String getHeaderName(HttpServletRequest req, String findKey){
        Enumeration headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String key = (String) headerNames.nextElement();
            if(key.equals(findKey)) return req.getHeader(key);
        }
        return "";
    }
}
